package org.jeecgframework.web.controller;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.Tree;
import org.jeecgframework.core.common.model.json.TreeGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.ZyEmValidaRuleServiceI;
import org.jeecgframework.web.system.pojo.base.ZyEmValidaRuleEntity;
import org.jeecgframework.web.system.pojo.base.ZyEquipmentTypeEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



/**   
 * @Title: Controller
 * @Description: 设备模型验证规则
 * @author onlineGenerator
 * @date 2016-04-27 16:34:18
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/zyEmValidaRuleController")
public class ZyEmValidaRuleController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZyEmValidaRuleController.class);

	@Autowired
	private ZyEmValidaRuleServiceI zyEmValidaRuleService;
	@Autowired
	private SystemService systemService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 设备模型验证规则列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "zyEmValidaRule")
	public ModelAndView zyEmValidaRule(HttpServletRequest request) {
		return new ModelAndView("com/zybk/zy_em_valida_rule/zyEmValidaRuleList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ZyEmValidaRuleEntity zyEmValidaRule,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyEmValidaRuleEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyEmValidaRule, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.zyEmValidaRuleService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除设备模型验证规则
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ZyEmValidaRuleEntity zyEmValidaRule, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		zyEmValidaRule = systemService.getEntity(ZyEmValidaRuleEntity.class, zyEmValidaRule.getId());
		message = "设备模型验证规则删除成功";
		try{
			zyEmValidaRuleService.delete(zyEmValidaRule);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "设备模型验证规则删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除设备模型验证规则
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "设备模型验证规则删除成功";
		try{
			for(String id:ids.split(",")){
				ZyEmValidaRuleEntity zyEmValidaRule = systemService.getEntity(ZyEmValidaRuleEntity.class, 
				id
				);
				zyEmValidaRuleService.delete(zyEmValidaRule);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "设备模型验证规则删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	 /**
	 * 设备模型验证规则新增页面跳转
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(ZyEmValidaRuleEntity zyEmValidaRule, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(zyEmValidaRule.getId())) {
			zyEmValidaRule = zyEmValidaRuleService.getEntity(ZyEmValidaRuleEntity.class, zyEmValidaRule.getId());
			req.setAttribute("zyEmValidaRulePage", zyEmValidaRule);
		}
		return new ModelAndView("com/zybk/zy_em_valida_rule/zyEmValidaRule-add");
	}

	/**
	 * @author xiul
	 * 2016-4-27 下午5:00:42
	 * Description 设备模型验证规则 - 选择设备类型 
	 * @param request
	 * @return
	 */
	@RequestMapping(params="goChooseEquipmentType")
	public ModelAndView goChooseEquipmentType(HttpServletRequest request){
		return new ModelAndView("com/zybk/zy_em_valida_rule/zyChooseEquipmentTypeForEmValida");
	}
	
	/**
	 * @author xiul
	 * 2016-4-27 下午5:04:08
	 * Description 设备模型验证规则 - 选择设备类型datagrid加载
	 * @param zyEquipmentTypeEntity
	 * @param request
	 * @param response
	 */
	@RequestMapping(params="dataGridOfChooseEquipmentType")
	@ResponseBody
	public List<Tree> dataGridOfChooseEquipmentType(ZyEquipmentTypeEntity zyEquipmentTypeEntity,HttpServletRequest request,HttpServletResponse response,TreeGrid treegrid){
		CriteriaQuery cq;
		List<Tree> treeGrids = new ArrayList<Tree>();
		cq = new CriteriaQuery(ZyEquipmentTypeEntity.class);
		if (treegrid.getId() != null) {
			cq.eq("equipmentType.id", treegrid.getId());
		}else if(treegrid.getId() == null){
			cq.isNull("equipmentType.id");
		}
		cq.add();
        List<ZyEquipmentTypeEntity> ZyEquipmentTypeList = systemService.getListByCriteriaQuery(cq, false);
        /*TreeGridModel treeGridModel = new TreeGridModel();
        treeGridModel.setIdField("id");
		treeGridModel.setTextField("typeDesc");
		treeGridModel.setParentId("equipmentType_id");
		treeGridModel.setParentText("equipmentType_typeDesc");
		treeGridModel.setSrc("");
		treeGridModel.setChildList("childEquipmentTypes");
		treeGrids = systemService.treegrid(ZyEquipmentTypeList, treeGridModel);*/
		Tree  t = new Tree("0","设备机型");
		assembleTree(t,ZyEquipmentTypeList);
		List<Tree> treeList = new ArrayList<>();
		treeList.add(t);
		return treeList;
	}

	public void assembleTree(Tree tree, List<ZyEquipmentTypeEntity> types) {
		Collections.sort(types, new Comparator<ZyEquipmentTypeEntity>() {
			@Override
			public int compare(ZyEquipmentTypeEntity o1, ZyEquipmentTypeEntity o2) {
				if (null == o1.getEquipmentType()) {
					return -1;
				}
				if (null == o2.getEquipmentType()) {
					return 1;
				}
				return 2;
			}
		});
		if (null == tree) {
			tree = new Tree();
			tree.setId("0");
			tree.setText("root");
		} else {
			for (ZyEquipmentTypeEntity type : types) {
				Tree t = new Tree(type.getId(), type.getTypeDesc());
				tree.getChildren().add(t);
				if (type.getChildEquipmentTypes().size() > 0) {
					assembleTree(t, type.getChildEquipmentTypes());
				}
			}
		}
	}

	/**
	 * 添加设备模型验证规则
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(ZyEmValidaRuleEntity zyEmValidaRule, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "设备模型验证规则添加成功";
		try{
			zyEmValidaRuleService.save(zyEmValidaRule);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "设备模型验证规则添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 更新设备模型验证规则
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(ZyEmValidaRuleEntity zyEmValidaRule, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "设备模型验证规则更新成功";
		ZyEmValidaRuleEntity t = zyEmValidaRuleService.get(ZyEmValidaRuleEntity.class, zyEmValidaRule.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(zyEmValidaRule, t);
			zyEmValidaRuleService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "设备模型验证规则更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 设备模型验证规则编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(ZyEmValidaRuleEntity zyEmValidaRule, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(zyEmValidaRule.getId())) {
			zyEmValidaRule = zyEmValidaRuleService.getEntity(ZyEmValidaRuleEntity.class, zyEmValidaRule.getId());
			req.setAttribute("zyEmValidaRulePage", zyEmValidaRule);
			if(StringUtil.isNotEmpty(zyEmValidaRule.getEquipmentTypeId())){
				ZyEquipmentTypeEntity zyEquType = systemService.get(ZyEquipmentTypeEntity.class, zyEmValidaRule.getEquipmentTypeId());
				req.setAttribute("zyEquType", zyEquType);
			}
		}
		return new ModelAndView("com/zybk/zy_em_valida_rule/zyEmValidaRule-update");
	}
	
	
}
